package com.yanqu.road.web.redis.subpub.impl;

import com.yanqu.road.reids.subpub.ReliableMessageSubPub;
import com.yanqu.road.reids.subpub.eRedisChannelType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.su.ReloadManager;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;

import java.util.List;

public class CenterManagerReloadSubPub implements ReliableMessageSubPub {

    String channel = eRedisChannelType.CenterManagerReload.getChannel();

    @Override
    public void onMessage(String message) {
        TempMgr.getLogger().info("{}", message);
        List<String> list = StringUtils.stringToStringList(message, ";");
        if(list.size() > 1){
            String mgrName = list.get(1);
            ReloadManager reloadManager = new ReloadManager(WebServer.getInstance());
            boolean reloadConfig = reloadManager.reloadConfig(mgrName);
            if(reloadConfig){
                TempMgr.getLogger().info("reload {} success", mgrName);
            }
        }
    }

    @Override
    public boolean isSupport(String channel) {
        return this.channel.equals(channel);
    }

    @Override
    public String getChannel() {
        return this.channel;
    }

    @Override
    public String getKey(String message) {
        return eRedisChannelType.CenterManagerReload.getMessageKey(message);
    }
}
